

'--------------------------------------------------------
' common
'--------------------------------------------------------
' 创建用于测试的 entity 管理?

Private Function createEm() As ISqlEntityManager
    Dim entityInfoSheetNameVal As String
    entityInfoSheetNameVal = entityInfoSheetName.value

    Dim itemInfoSheetNameVal As String
    itemInfoSheetNameVal = itemInfoSheetName.value

    If Not UtilsWorkbook.hasSheet(ActiveWorkbook, entityInfoSheetNameVal) Then
        Err.Raise 9999, Description:="can not find entityInfoSheet:[" & entityInfoSheetNameVal & "] in ActiveWorkbook"
        Exit Function
    End If

    If Not UtilsWorkbook.hasSheet(ActiveWorkbook, itemInfoSheetNameVal) Then
        Err.Raise 9999, Description:="can not find itemInfoSheetName:[" & itemInfoSheetNameVal & "] in ActiveWorkbook"
        Exit Function
    End If

    
    Dim entityInfoSheet As Worksheet
    Set entityInfoSheet = ActiveWorkbook.Sheets(entityInfoSheetNameVal)
    Dim itemInfoSheet As Worksheet
    Set itemInfoSheet = ActiveWorkbook.Sheets(itemInfoSheetNameVal)

    ' entity sheet const
    Dim entityInfoStartRowVal As Long
    Dim entityLogicNameColVal As Variant
    Dim entityPhyNameColVal As Variant
    
    If entityInfoStartRow.value = "" Then
        entityInfoStartRowVal = -1
    ElseIf Not IsNumeric(entityInfoStartRow.value) Then
        Err.Raise 9998, Description:="entityInfoStartRow must be a number"
        Exit Function
    Else
        entityInfoStartRowVal = entityInfoStartRow.value
    End If
    
    If entityLogicNameCol.value = "" Then
        entityLogicNameColVal = -1
    Else
        entityLogicNameColVal = entityLogicNameCol.value
    End If
    
    If entityPhyNameCol.value = "" Then
        entityPhyNameColVal = -1
    Else
        entityPhyNameColVal = entityPhyNameCol.value
    End If
    
    ' item sheet const
    Dim itemInfoStartRowVal As Long
    Dim itemPhyNameColVal As Variant
    Dim itemLogicNameColVal As Variant
    Dim itemEntityLogicNameColVal As Variant
    Dim itemEntityPhyNameColVal As Variant

    If itemInfoStartRow.value = "" Then
        itemInfoStartRowVal = -1
    ElseIf Not IsNumeric(itemInfoStartRow.value) Then
        Err.Raise 9998, Description:="itemInfoStartRow must be a number"
        Exit Function
    Else
        itemInfoStartRowVal = itemInfoStartRow.value
    End If
    
    If itemPhyNameCol.value = "" Then
        itemPhyNameColVal = -1
    Else
        itemPhyNameColVal = itemPhyNameCol.value
    End If
    
    If itemLogicNameCol.value = "" Then
        itemLogicNameColVal = -1
    Else
        itemLogicNameColVal = itemLogicNameCol.value
    End If
    
    If itemEntityLogicNameCol.value = "" Then
        itemEntityLogicNameColVal = -1
    Else
        itemEntityLogicNameColVal = itemEntityLogicNameCol.value
    End If
    
    If itemEntityPhyNameCol.value = "" Then
        itemEntityPhyNameColVal = -1
    Else
        itemEntityPhyNameColVal = itemEntityPhyNameCol.value
    End If

    ' create em
    Dim oneSheetEntityManager As SeparateOneSheetEntityManager
    Set oneSheetEntityManager = New SeparateOneSheetEntityManager

    oneSheetEntityManager.Init _
        entityInfoSheet, _
        itemInfoSheet, _
        entityInfoStartRow:=entityInfoStartRowVal, _
        entityLogicNameCol:=entityLogicNameColVal, _
        entityPhyNameCol:=entityPhyNameColVal, _
        itemInfoStartRow:=itemInfoStartRowVal, _
        itemPhyNameCol:=itemPhyNameColVal, _
        itemLogicNameCol:=itemLogicNameColVal, _
        itemEntityLogicNameCol:=itemEntityLogicNameColVal, _
        itemEntityPhyNameCol:=itemEntityPhyNameColVal

    Set createEm = oneSheetEntityManager
End Function

'--------------------------------------------------------
' button event
'--------------------------------------------------------
Private Sub ToLogicSqlBtn_Click()
    Dim originSqlVal As String
    originSqlVal = originSql.value
    If originSqlVal = "" Then
        Exit Sub
    End If

    Dim em As ISqlEntityManager
    Set em = createEm
    
    Dim translator As SQLTranslator
    Set translator = New SQLTranslator
    translator.Init em
    
    translatedSql.value = translator.translateToLogicSql(originSqlVal, tips.value)
End Sub

Private Sub ToPhySqlBtn_Click()
    Dim originSqlVal As String
    originSqlVal = originSql.value
    If originSqlVal = "" Then
        Exit Sub
    End If

    Dim em As ISqlEntityManager
    Set em = createEm
    
    Dim translator As SQLTranslator
    Set translator = New SQLTranslator
    translator.Init em
    
    translatedSql.value = translator.translateToPhySql(originSqlVal, tips.value)
End Sub
